package service;

import entity.Post;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;
import javax.validation.ConstraintViolation;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 *
 * @author Wink
 */
@Stateless
@Path("posts")
public class PostsFacadeREST extends AbstractFacade<Post> {
    @PersistenceContext(unitName = "HashArea-warPU")
    private EntityManager em;

    public PostsFacadeREST() {
        super(Post.class);
    }

    @POST
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public Post createPost(Post entity) {
        System.out.println("creating post");
        try {
            em.persist(entity);
        } catch (javax.validation.ConstraintViolationException e) {
            Set<ConstraintViolation<?>> cv = e.getConstraintViolations();
            for (ConstraintViolation c : cv) {
                System.out.println ("violation: " + c.getMessage());
            }
        }
        em.flush();
        return entity;
    }

    @PUT
    @Override    
    @Produces({"application/json", "application/xml"})
    public void edit(Post post) {
        super.edit(post);
    }

    @DELETE
    public void remove(@QueryParam("id") Long id) {
        super.remove(super.find(id));
    }
 
    /**
     * Retrieve Posts from storage that match query parameters.
     * @param board_id id of board that posts are stored on.
     * @param from_when Earliest date that the post could be created on (default value 2012).
     * @param to_when Latest date that the post could be created on (default value 9999).
     * @return List<Post> containing resulting posts.
     */
    @GET
    @Produces({"application/json", "application/xml"})
    public List<Post> find(@QueryParam("board_id") int board_id,
    				@DefaultValue("2012-12-31 12:00:00") @QueryParam("from_when") String from_when,
    				@DefaultValue("9999-12-31 12:00:00") @QueryParam("to_when") String to_when) {
        System.out.println("Get post request");
        try {
            ArrayList<Post> lp = new ArrayList<Post>();
            lp.addAll(em.createNamedQuery("Post.find")
                .setParameter("board_id", board_id)
                .setParameter("from_when", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH)
                    .parse(from_when), TemporalType.DATE)
                .setParameter("to_when", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH)
                    .parse(to_when), TemporalType.DATE)
                .getResultList());
            System.out.println("Sending " + lp.size() + "posts");
            return lp;
        } catch (ParseException e) {
            return null;
        }       
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}